package Member.selevrct;

import Member.entiy.Member;
import Member.servise.MemberSers;
import Member.servise.impl.MemberSerImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/MemberServletAdd")
public class MemberServletAdd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String statusyuStr = request.getParameter("statusyu");
        int statusyu=statusyuStr!=null?Integer.parseInt(statusyuStr):0;
        MemberSers ms=new MemberSerImpl();

        Member member=new Member();
        member.setName(name);
        member.setPhoneNumber(phone);
        member.setEmail(email);
        member.setLevel(statusyu);

        PrintWriter out = response.getWriter();
        int in = ms.addInfor(member);

        if (in > 0) {
            out.println("<script> alert(\"添加成功\") </script>");
            response.sendRedirect("member.jsp");
        } else {
            out.print("<script>alert(\"添加失败\")</script>");
            response.sendRedirect("memberTj.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
